#ifndef SHNETDEBUGGER_H
#define SHNETDEBUGGER_H

#include <QObject>
#include <QHostAddress>

class QUdpSocket;
class QtQuick2ApplicationViewer;

// can't be done in function because of QDataStream
/// @todo better solution without copy paste ?
#define sh_net_startmsg() QByteArray datagram; \
    QDataStream out(&datagram, QIODevice::WriteOnly); \
    out.setVersion(QDataStream::Qt_5_0); \
    out << (quint8)1; /*version*/ \
    out << m_nodeName;
#define sh_net_sendmsg() m_udp->writeDatagram(datagram.data(), \
    datagram.size(), m_groupAddress, m_port);

class ShNetDebugger : public QObject
{
    Q_OBJECT
public:
    explicit ShNetDebugger(QObject *parent = 0);
    
signals:
    
public slots:

private slots:
    void processDatagrams();
    void onJoinClicked();
    void setProperty(const QString &url);
    void execFunction(const QString &url);
    void propertyChange(const QString &url);

private:
    bool join(const QHostAddress &interface,
              const QHostAddress &multicastGroup, quint16 port,
              const QString &nodeName);
    QUdpSocket *m_udp;
    quint16 m_port;
    QHostAddress m_groupAddress;
    enum FRAME_TYPE {FUNCTION_CALL,
                     FUNCTION_CALL_RESULT,
                     PROPERTY_SET,
                     PROPERY_CHANGE_NOTIFY,
                     NETWORK_SIGNAL};

    //
    // info
    //
    QString m_nodeName;

    //qml
    QtQuick2ApplicationViewer *m_qml;

    // statistic
    void outStatistic(int outBytes);
    int m_inPackets;
    int m_outPackets;
    int m_inBytes;
    int m_outBytes;
};

#endif // SHNETDEBUGGER_H
